package cc.imos.repository;

import cc.imos.entity.SysAccount;
import cc.imos.work.LoginRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by asher on 2017/4/23.
 */
@Repository
@CacheConfig(cacheNames = {"AccountMapper"})
public class AccountRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    //    @Transactional(readOnly = true)
    public SysAccount findForLogin(LoginRequest loginRequest) {
        String sql = "SELECT * FROM sys_account WHERE flag = ? AND keyword = ? AND password = ?";
        Object[] args = new Object[] { true, loginRequest.getKeyword(), loginRequest.getPassword() };
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SysAccount.class), args);
//        String sql = "SELECT * FROM sys_account WHERE keyword = :keyword";
//        SqlParameterSource paramSource = new BeanPropertySqlParameterSource(loginRequest);
//        return namedParameterJdbcTemplate.queryForObject(sql, paramSource, new BeanPropertyRowMapper<>(SysAccount.class));
//        String sql = "SELECT * FROM sys_account WHERE keyword = ?";
//        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SysAccount.class), loginRequest.getKeyword());
    }

    public int totalRow(Map<String, Object> args) {
        return 0;
    }

    public List<Map<String, Object>> list(Map<String, Object> args){
        return null;

    }

    // @CacheEvict(cacheNames = {"AccountMapper"})
    public long insert( Map<String, Object> args) {
        return 1L;
    }


    //    @Cacheable
    public List<Map<String, Object>> listRole(Map<String, Object> args) {
        return null;
    }

}
